/**
 * Company: Easy with
 * Official website: http://www.easyadmin.site
 * project: sec-bigdata
 * Author:  Yang Huijing  2021-08-19
 * Email: 1073602@qq.com
 */
package com.easy.framework.common.utils;


import lombok.SneakyThrows;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 断言工具
 * Author:  Yang Huijing
 */
public class AssertUtil {

    public AssertUtil() {
    }

    @SneakyThrows
    public static <E extends Exception> void throwException(Class<E> clazz, String message) {
        try {
            // 获取异常类的String参数构造函数
            Constructor<E> constructor = clazz.getConstructor(String.class);
            // 创建异常实例并抛出
            throw constructor.newInstance(message);
        } catch (NoSuchMethodException e) {
            // 如果没有String参数的构造函数
            throw new RuntimeException("Exception class " + clazz.getName() + " does not have a String constructor", e);
        } catch (InstantiationException e) {
            // 无法实例化（抽象类等）
            throw new RuntimeException("Cannot instantiate exception class " + clazz.getName(), e);
        } catch (IllegalAccessException e) {
            // 构造函数不可访问
            throw new RuntimeException("Constructor of " + clazz.getName() + " is not accessible", e);
        } catch (InvocationTargetException e) {
            // 构造函数本身抛出异常
            Throwable targetException = e.getTargetException();
            if (targetException instanceof RuntimeException) {
                throw (RuntimeException) targetException;
            } else {
                throw new RuntimeException("Exception occurred while creating instance of " + clazz.getName(), targetException);
            }
        }
    }

    @SneakyThrows
    public static <E extends Exception> void isTrue(boolean expression, E exception) {
        if (!expression) {
            throw exception;
        }
    }

    @SneakyThrows
    public static <E extends Exception> void isFalse(boolean expression, E exception) {
        if (expression) {
            throw exception;
        }
    }

    @SneakyThrows
    public static <E extends Exception> void isNull(Object object, E exception) {
        if (object != null) {
            throw exception;
        }
    }

    @SneakyThrows
    public static <E extends Exception> void isNotNull(Object object, E exception) {
        if (object == null) {
            throw exception;
        }
    }

    @SneakyThrows
    public static <E extends Exception> void isEmpty(Object object, E exception) {
        if (Func.isNotEmpty(object)) {
            throw exception;
        }
    }

    @SneakyThrows
    public static <E extends Exception> void isNotEmpty(Object object, E exception) {
        if (Func.isEmpty(object)) {
            throw exception;
        }
    }


    public static <E extends Exception> void isTrue(boolean expression, Class<E> clazz, String msg) {
        if (!expression) {
            throwException(clazz, msg);
        }
    }

    public static <E extends Exception> void isFalse(boolean expression, Class<E> clazz, String msg) {
        if (expression) {
            throwException(clazz, msg);
        }
    }

    public static <E extends Exception> void isNull(Object object, Class<E> clazz, String msg) {
        if (object != null) {
            throwException(clazz, msg);
        }
    }

    public static <E extends Exception> void isNotNull(Object object, Class<E> clazz, String msg) {
        if (object == null) {
            throwException(clazz, msg);
        }
    }

    public static <E extends Exception> void isEmpty(Object object, Class<E> clazz, String msg) {
        if (Func.isNotEmpty(object)) {
            throwException(clazz, msg);
        }
    }

    public static <E extends Exception> void isNotEmpty(Object object, Class<E> clazz, String msg) {
        if (Func.isEmpty(object)) {
            throwException(clazz, msg);
        }
    }

    public static void isTrue(boolean expression, String msg) {
        isTrue(expression, new IllegalArgumentException(msg));
    }

    public static void isFalse(boolean expression, String msg) {
        isFalse(expression, new IllegalArgumentException(msg));
    }

    public static void isNull(Object object, String msg) {
        isNull(object, new IllegalArgumentException(msg));
    }

    public static void isNotNull(Object object, String msg) {
        isNotNull(object, new IllegalArgumentException(msg));
    }

    public static void isEmpty(Object object, String msg) {
        isEmpty(object, new IllegalArgumentException(msg));
    }

    public static void isNotEmpty(Object object, String msg) {
        isNotEmpty(object, new IllegalArgumentException(msg));
    }
}
